import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CinemaSystemV1_1 {
    private static UserManager userManager = new UserManager();
    private static MovieManager movieManager = new MovieManager();
    private static ScheduleManager scheduleManager = new ScheduleManager();
    private static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("\n=== 影院管理系统 V1.1 ===");
            System.out.println("1. 登录");
            System.out.println("2. 注册新用户");
            System.out.println("3. 列出用户");
            System.out.println("0. 退出");

            String choice = sc.nextLine();
            switch (choice) {
                case "1": login(); break;
                case "2": register(); break;
                case "3": userManager.listUsers(); break;
                case "0":
                    System.out.println("系统退出，保存数据...");
                    userManager.saveUsers(); movieManager.saveMovies();
                    return;
                default: System.out.println("无效选择！");
            }
        }
    }

    private static void login() {
        System.out.print("用户名: ");
        String uname = sc.nextLine();
        System.out.print("密码: ");
        String pwd = sc.nextLine();
        User u = userManager.login(uname, pwd);
        if (u == null) { System.out.println("登录失败！"); return; }
        System.out.println("欢迎 " + u.getUsername() + " (" + u.getRole() + ")");
        if (u.getRole().equals("manager")) managerMenu();
        else if (u.getRole().equals("cashier")) cashierMenu();
        else System.out.println("管理员功能请后续扩展");
    }

    private static void register() {
        System.out.print("用户名: "); String name = sc.nextLine();
        System.out.print("密码: "); String pwd = sc.nextLine();
        System.out.print("角色(admin/manager/cashier): "); String role = sc.nextLine();
        System.out.print("手机号: "); String phone = sc.nextLine();
        userManager.register(name, pwd, role, phone);
        System.out.println("注册成功！");
    }

    // ================== 经理菜单 ==================
    private static void managerMenu() {
        while (true) {
            System.out.println("\n=== 经理菜单 ===");
            System.out.println("1. 添加影片");
            System.out.println("2. 查看影片");
            System.out.println("3. 安排场次");
            System.out.println("4. 查看场次");
            System.out.println("0. 退出登录");
            String c = sc.nextLine();
            switch (c) {
                case "1":
                    System.out.print("片名: "); String n = sc.nextLine();
                    System.out.print("导演: "); String d = sc.nextLine();
                    System.out.print("主演: "); String a = sc.nextLine();
                    System.out.print("简介: "); String desc = sc.nextLine();
                    System.out.print("时长(分钟): "); int dur = Integer.parseInt(sc.nextLine());
                    System.out.print("评分: "); double s = Double.parseDouble(sc.nextLine());
                    movieManager.addMovie(new Movie(n, d, a, desc, dur, s));
                    System.out.println("影片已添加！");
                    break;
                case "2": movieManager.listMovies(); break;
                case "3":
                    System.out.print("片名: "); String mn = sc.nextLine();
                    Movie m = movieManager.findMovie(mn);
                    if (m == null) { System.out.println("未找到影片！"); break; }
                    System.out.print("影厅: "); String hall = sc.nextLine();
                    System.out.print("时间: "); String t = sc.nextLine();
                    System.out.print("票价: "); double price = Double.parseDouble(sc.nextLine());
                    System.out.print("总座位数: "); int seat = Integer.parseInt(sc.nextLine());
                    scheduleManager.addSchedule(new Schedule(mn, hall, t, price, seat));
                    System.out.println("场次已添加！");
                    break;
                case "4": scheduleManager.listSchedules(); break;
                case "0": return;
                default: System.out.println("无效选择！");
            }
        }
    }
    private static void cashierMenu() {
        while (true) {
            System.out.println("\n=== 前台菜单 ===");
            System.out.println("1. 查看影片");
            System.out.println("2. 查看场次");
            System.out.println("3. 售票");
            System.out.println("4. 退票");
            System.out.println("0. 退出登录");
            String c = sc.nextLine();
            switch (c) {
                case "1": movieManager.listMovies(); break;
                case "2": scheduleManager.listSchedules(); break;
                case "3":
                    System.out.print("输入场次ID: "); int id = Integer.parseInt(sc.nextLine());
                    Schedule sch = scheduleManager.findById(id);
                    if (sch == null) { System.out.println("无效场次！"); break; }
                    System.out.print("选择座位号: "); int seat = Integer.parseInt(sc.nextLine());
                    if (sch.sellSeat(seat)) System.out.println("购票成功！");
                    else System.out.println("购票失败，座位已售！");
                    break;
                case "4":
                    System.out.print("输入场次ID: "); int id2 = Integer.parseInt(sc.nextLine());
                    Schedule sch2 = scheduleManager.findById(id2);
                    if (sch2 == null) { System.out.println("无效场次！"); break; }
                    System.out.print("输入退票座位号: "); int s2 = Integer.parseInt(sc.nextLine());
                    if (sch2.refundSeat(s2)) System.out.println("退票成功！");
                    else System.out.println("退票失败！");
                    break;
                case "0": return;
                default: System.out.println("无效选择！");
            }
        }
    }
}

